home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13760 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.7 KB  |  202 lines

  1. Path: newsfeed.internetmci.com!rbdc!usenet
  2. From: marty1@rbdc.rbdc.com (Jumping Jack Flash)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help with a very weird C problem
  5. Date: Wed, 10 Apr 1996 06:02:42 GMT
  6. Organization: Red Barn Data Center, Winston-Salem, NC.
  7. Message-ID: <316b4a54.48010904@199.171.83.2>
  8. References: <31620c87.184254741@199.171.83.2> <4k2tdl$ft4@preeda.internex.net.au> <828718259snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: rbdc8.rbdc.com
  10. X-Newsreader: Forte Agent .99d/32.168
  11.  
  12. On Fri, 05 Apr 96 15:30:59 GMT, Lawrence Kirby
  13. <fred@genesis.demon.co.uk> wrote:
  14.  
  15. -In article <4k2tdl$ft4@preeda.internex.net.au>
  16. -           sultan@connexus.apana.org.au "Jon Hornstein" writes:
  17. -
  18. ->With the briefest of looks at the code this function 
  19. ->
  20. ->
  21. ->>char *PkunzipDir(char *curdir,char *name2,char *file)
  22. ->>{
  23. ->>char pkunzip2[120] = "pkunzip ";
  24. ->>strcat(pkunzip2,curdir);
  25. ->>strcat(pkunzip2,name2);
  26. ->>strcat(pkunzip2," ");
  27. ->>strcat(pkunzip2,curdir);
  28. ->>strcat(pkunzip2,file);
  29. ->>return(pkunzip2);
  30. ->>}
  31. ->
  32. ->should read
  33. ->
  34. ->
  35. ->char *PkunzipDir(char *curdir,char *name2,char *file)
  36. ->{
  37. ->static char pkunzip2[120] = "pkunzip ";
  38. ->
  39. ->    strcat(pkunzip2,curdir);
  40. ->    strcat(pkunzip2,name2);
  41. ->    strcat(pkunzip2," ");
  42. ->    strcat(pkunzip2,curdir);
  43. ->    strcat(pkunzip2,file);
  44. ->    return(pkunzip2);
  45. ->}
  46. -
  47. -That would fail if you called PkunzipDir() more than once.
  48. -
  49. -Or you could write it more simply and clearly (and correctly) as:
  50. -
  51. -char *PkunzipDir(char *curdir,char *name2,char *file)
  52. -{
  53. -    static char pkunzip2[120];
  54. -
  55. -    sprintf(pkunzip2, "pkunzip %s%s %s%s", curdir, name2, curdir,
  56. file);
  57. -
  58. -    return pkunzip2;
  59. -}
  60. -
  61. -sprintf is extremely useful for string handling.
  62. -
  63. -It is generally better to have the calling function pass a pointer to
  64. a
  65. -suitable buffer than to return a pointer to a static buffer.
  66. ------------------------------------------
  67. -Lawrence Kirby | fred@genesis.demon.co.uk
  68. -Wilts, England | 70734.126@compuserve.com
  69. ------------------------------------------
  70.  
  71. Here's where I'm at so far. Good Bad or Ugly. But it works like
  72. I want it to and is easier to read.
  73.  
  74. #include <stdlib.h>
  75. #include <conio.h>
  76. #include <iostream.h>
  77. #include <string.h>
  78. #include <dir.h>
  79. #include <process.h>
  80. #include <stdio.h>
  81. #include <errno.h>
  82. #define TRUE 1
  83. #define FALSE 0
  84.  
  85. char *PkunzipDir(char *curdir,char *name2,char *file);
  86. char *Current_Directory(char *path);
  87. void PkunzipSearch(void);
  88.  
  89. int main(int argc, char **argv)
  90. {
  91. char drive[MAXDRIVE];
  92. char dir[MAXDIR];
  93. char file[MAXFILE];
  94. char ext[MAXEXT];
  95. char curdir[MAXPATH];
  96. struct ffblk ffblk;
  97. int done, stat;
  98.  
  99.  
  100. clrscr();//Clears Screen
  101. PkunzipSearch();//Searches for pkunzip.exe file
  102. Current_Directory(curdir);//Gets Current Directory
  103.  
  104. if(argc == 2)
  105.     {
  106.     done = findfirst(argv[1],&ffblk,0);
  107.     }
  108. else
  109.     {
  110.     done = findfirst("*.zip",&ffblk,0);
  111.     }
  112.  
  113. if(done)
  114.     {
  115.      clrscr();
  116.      cout << "No Zipped files in current Directory\n";
  117.     }
  118. else//A2
  119.     {
  120.     while (!done)
  121.         {
  122.         fnsplit(ffblk.ff_name,drive,dir,file,ext);
  123.         stat = mkdir(file);
  124.         if (!stat)
  125.             {
  126.             cout << "\n" << "Directory " << file << "
  127. created\n";
  128.             cout << "Unzipping Files into it\n";
  129.  
  130. if(system(PkunzipDir(curdir,ffblk.ff_name,file)))
  131.                 {
  132.                 cout << "ERROR\n";
  133.                 exit(EXIT_FAILURE);
  134.                 }
  135.             }
  136.         else
  137.             {
  138.             clrscr();
  139.             cout << "Unable to create directory " << file
  140. << "\n";
  141.             cout << "For zipped file " << file << ext;
  142.             }
  143.         done = findnext(&ffblk);
  144.         }//Ends While
  145.     }//Ends Else A2
  146. cout <<"\n" << "This program is Freeware\n";
  147. cout << "Written by Marty A. Lineberry\n";
  148. cout << "Internet address marty1@rbdc.rbdc.com\n";
  149. cout << "End of Program\n";
  150. return 0;
  151. }
  152.  
  153.  
  154. //**********************************************************
  155. char *PkunzipDir(char *curdir,char *name2,char *file)
  156. {
  157. static char pkunzip2[120];    //Keeps in mem       
  158. pkunzip2[0] = '\0';   //Sets array to 0 bytes
  159. strcat(pkunzip2,"pkunzip "); 
  160. strcat(pkunzip2,curdir);
  161. strcat(pkunzip2,name2);
  162. strcat(pkunzip2," ");
  163. strcat(pkunzip2,curdir);
  164. strcat(pkunzip2,file);
  165. return(pkunzip2);
  166. }
  167.  
  168. //**********************************************************
  169. //* Used to get the current Drive & Directory              *
  170. //**********************************************************
  171. char *Current_Directory(char *path)
  172. {
  173.    strcpy(path, "X:\\");      /* fill string with form of response:
  174. X:\ */
  175.    path[0] = 'A' + getdisk();    /* replace X with current drive
  176. letter */
  177.    getcurdir(0, path+3);  /* fill rest of string with current
  178. directory */
  179.    if(strlen(path) > 3) /* Puts ending / if not root directory */
  180.     {
  181.     strcat(path,"\\");
  182.     }
  183.    return(path);
  184. }
  185.  
  186.  
  187. //*************************************************************
  188. void PkunzipSearch(void)
  189. {
  190. char *pkunzip_search;
  191. //Searches all current dos paths for pkunzip
  192. pkunzip_search = searchpath("pkunzip.exe");
  193. if(pkunzip_search == NULL)
  194.     {
  195.     clrscr();
  196.     cout << "PKUNZIP.EXE must be in your path!";
  197.     exit(EXIT_FAILURE);
  198.     }
  199. return;
  200. }
  201.  
  202.